package geektime.concurrent.race;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MyCompute implements ComputeRunnable {

	MyShareData ssd;
	int size;
	int offset;
	public MyCompute(MyShareData ssd, int size, int offset) {
		this.ssd = ssd;
		this.size = size;
		this.offset = offset;
	}

	@Override
	public void run() {
		go();

	}

	@Override
	public void go() {
		System.out.println("开始计算随机数: " + size + " " + offset);
		int start = offset * size;
		List<Integer> subList = ssd.getScore().subList(start, start+size);
		subList.sort(Collections.reverseOrder());

		for (int i = 0; i < SimpleShareData.BUFSIZE * size / SimpleShareData.COUNT; i++) {
			//System.out.println("随机数: " + alist[alist.length - i - 1]);
			ssd.addExchange(subList.get(i));
		}
		ssd.getCompSig().countDown();;
		//System.out.println("计算随机数完毕: " + size + " " + SimpleShareData.BUFSIZE * size / SimpleShareData.COUNT);
	}
}
